package com.yocard.yxyd.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.yocard.yxyd.R;
import com.yocard.yxyd.http.model.response.CommonBean;
import com.yocard.yxyd.others.Glide4Engine;
import com.yocard.yxyd.ui.activity.base.BaseActivity;
import com.yocard.yxyd.ui.contract.ChargeContract;
import com.yocard.yxyd.ui.presenter.ChargePresenter;
import com.yocard.yxyd.utils.util.ActivityUtils;
import com.yocard.yxyd.utils.util.ConvertUtils;
import com.yocard.yxyd.utils.util.KeyboardUtils;
import com.yocard.yxyd.utils.util.StringUtils;
import com.yocard.yxyd.utils.util.ToastUtils;
import com.zhihu.matisse.Matisse;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created on 2019/4/9.
 */
public class ChargebackActivity extends BaseActivity<ChargePresenter> implements ChargeContract.View {

    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_lenth)
    TextView tvLenth;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_1)
    ImageView iv1;
    @BindView(R.id.iv_2)
    ImageView iv2;
    @BindView(R.id.iv_3)
    ImageView iv3;
    @BindView(R.id.iv_1_close)
    ImageView iv1Close;
    @BindView(R.id.iv_2_close)
    ImageView iv2Close;
    @BindView(R.id.iv_3_close)
    ImageView iv3Close;
    @BindView(R.id.et_remark)
    AppCompatEditText etRemark;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;

    final int SELECT_POS_1 = 0x11;
    final int SELECT_POS_2 = 0x22;
    final int SELECT_POS_3 = 0x33;

    final String KEY_TONGHUA = "tonghua";
    final String KEY_LIAOTIAN = "liaotian";
    final String KEY_OTHER = "other";

    Glide4Engine glide4Engine;
    Map<String, String> urlMap = new HashMap<>();
    protected String id;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.act_charge_back;
    }

    @Override
    protected void initEventAndData() {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            id = extras.getString("id");
        }
        initTitle();
        glide4Engine = Glide4Engine.getInstance();
        etRemark.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String trim = s.toString().trim();
                int length = s.toString().trim().length();
                tvLenth.setText(length + "/50");
                if (!StringUtils.isEmpty(trim) && urlMap.size() > 0) {
                    btnSubmit.setEnabled(true);
                } else {
                    btnSubmit.setEnabled(false);
                }
            }
        });
    }

    private void initTitle() {
        ImmersionBar.with(this).statusBarColor(R.color.white)
                .statusBarDarkFont(true).navigationBarColor(R.color.white).init();
        tvRight.setText("提交");
        tvRight.setVisibility(View.GONE);
        btnSubmit.setEnabled(false);
        tvTitle.setText("退单申请");
    }

    @OnClick({R.id.iv_back, R.id.iv_1, R.id.iv_2, R.id.iv_3, R.id.iv_1_close, R.id.iv_2_close
            , R.id.iv_3_close, R.id.btn_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                //防止短时间重复点击
                if (isFastDoubleClick()) return;

                commitRefund();
                break;
            case R.id.iv_back:
                ActivityUtils.finishActivity(this);
                break;
            case R.id.iv_1:
                mPresenter.selectImage(mContext, SELECT_POS_1);
                break;
            case R.id.iv_2:
                mPresenter.selectImage(mContext, SELECT_POS_2);
                break;
            case R.id.iv_3:
                mPresenter.selectImage(mContext, SELECT_POS_3);
                break;
            case R.id.iv_1_close:
                iv1.setImageResource(R.mipmap.ico_upload_img);
                iv1Close.setVisibility(View.GONE);
                urlMap.remove(KEY_TONGHUA);
                caseCommitStatus();
                break;
            case R.id.iv_2_close:
                iv2.setImageResource(R.mipmap.ico_upload_img);
                iv2Close.setVisibility(View.GONE);
                urlMap.remove(KEY_LIAOTIAN);
                caseCommitStatus();
                break;
            case R.id.iv_3_close:
                iv3.setImageResource(R.mipmap.ico_upload_img);
                iv3Close.setVisibility(View.GONE);
                urlMap.remove(KEY_OTHER);
                caseCommitStatus();
                break;
        }
    }

    private void commitRefund() {
        KeyboardUtils.hideSoftInput(this);
        String mark = etRemark.getText().toString().trim();
        if (!StringUtils.isEmpty(mark) && urlMap.size() > 0) {
            Collection<String> values = urlMap.values();
            List<String> list = new ArrayList<>();
            list.addAll(values);
            mPresenter.commitRefund(id, mark, list);
        } else {
            ToastUtils.showLong("请完善提交资料");
        }
    }

    private void caseCommitStatus() {
        String trim = etRemark.getText().toString().trim();
        if (!StringUtils.isEmpty(trim) && urlMap.size() > 0) {
            btnSubmit.setEnabled(true);
        } else {
            btnSubmit.setEnabled(false);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            List<Uri> uris = Matisse.obtainResult(data);
            List<String> paths = Matisse.obtainPathResult(data);

            showLoadingDialog();
            switch (requestCode) {
                case SELECT_POS_1:
//                    glide4Engine.loadThumbnail(mContext, ConvertUtils.dp2px(120), null, iv1, uris.get(0));
//                    iv1Close.setVisibility(View.VISIBLE);
                    mPresenter.uploadImage(KEY_TONGHUA, paths.get(0), iv1, uris.get(0));
                    break;
                case SELECT_POS_2:
//                    glide4Engine.loadThumbnail(mContext, ConvertUtils.dp2px(120), null, iv2, uris.get(0));
//                    iv2Close.setVisibility(View.VISIBLE);
                    mPresenter.uploadImage(KEY_LIAOTIAN, paths.get(0), iv2, uris.get(0));
                    break;
                case SELECT_POS_3:
//                    glide4Engine.loadThumbnail(mContext, ConvertUtils.dp2px(120), null, iv3, uris.get(0));
//                    iv3Close.setVisibility(View.VISIBLE);
                    mPresenter.uploadImage(KEY_OTHER, paths.get(0), iv3, uris.get(0));
                    break;
            }
        }
    }

    @Override
    public void uploadSuccess(String type, String picId, ImageView iv, Uri filePath) {
        glide4Engine.loadThumbnail(mContext, ConvertUtils.dp2px(120), null, iv, filePath);
        dismisDialog();
        urlMap.put(type, picId);

        String remark = etRemark.getText().toString().trim();
        if (!StringUtils.isEmpty(remark)) {
            btnSubmit.setEnabled(true);
        }
        if (iv.getId() == R.id.iv_1) {
            iv1Close.setVisibility(View.VISIBLE);
            return;
        }
        if (iv.getId() == R.id.iv_2) {
            iv2Close.setVisibility(View.VISIBLE);
            return;
        }
        if (iv.getId() == R.id.iv_3) {
            iv3Close.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onCommitSuccess() {
        EventBus.getDefault().post(new CommonBean(1));
        ActivityUtils.finishActivity(this);
    }

}
